home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / snip9503 / list.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-14  |  2.3 KB  |  160 lines

  1. /////////////////////////////////////////////////////////////
  2. // MODULE
  3. //  list.cpp
  4. // CREATED
  5. //  davidn  03 Dec 1994  23:59
  6. //  David L. Nugent
  7. //  This class implementation is donated to the public domain
  8. // DESCRIPTION
  9. //  Implementation of class node, list & iter
  10. // FUNCTIONS
  11. //  node::unlink()
  12. //    Destroys linkage from a list and removes it from a
  13. //    linked list
  14. //  node::link()
  15. //    Links a node into a linked list
  16. //  list::purge()
  17. //    Removes all linked list entries
  18. //  iter::traverse()
  19. //    Provides full tranversal functions for nodes in a
  20. //    linked list
  21. /////////////////////////////////////////////////////////////
  22.  
  23. // Implementation of class list & friends
  24.  
  25. #include "list.hpp"
  26.  
  27. void
  28. node::unlink()
  29. {
  30.   if ( mylist )
  31.   {
  32.       // Unlink from previous
  33.     if ( Prev )
  34.       Prev->Next = Next;
  35.     else
  36.       mylist->First = Next;
  37.  
  38.       // Unlink from next
  39.     if ( Next )
  40.       Next->Prev = Prev;
  41.     else
  42.       mylist->Last = Prev;
  43.  
  44.     mylist->nodes--;
  45.     mylist = 0;
  46.     Prev = Next = 0;
  47.   }
  48. }
  49.  
  50. void
  51. node::link( list * L, node * prv, node * nxt )
  52. {
  53.  
  54.     // If currently linked, then unlink it
  55.  
  56.   if ( mylist )
  57.     unlink();
  58.  
  59.     // Link it to the list
  60.  
  61.   if ( L )
  62.   {
  63.  
  64.     mylist = L;
  65.  
  66.     // Add after previous
  67.  
  68.     if ( prv )
  69.     {
  70.  
  71.       Prev = prv;
  72.       Next = prv->Next;
  73.     }
  74.  
  75.     // Add before next
  76.  
  77.     else if ( nxt )
  78.     {
  79.  
  80.       Next = nxt;
  81.       Prev = nxt->Prev;
  82.     }
  83.  
  84.     // Else just add to end
  85.  
  86.     else
  87.     {
  88.  
  89.       Next = 0;
  90.       Prev = L->Last;
  91.     }
  92.  
  93.     if ( Prev )
  94.       Prev->Next = this;
  95.     else
  96.       L->First = this;
  97.  
  98.     if ( Next )
  99.       Next->Prev = this;
  100.     else
  101.       L->Last = this;
  102.  
  103.     mylist->nodes++;
  104.   }
  105. }
  106.  
  107.  
  108. void
  109. list::purge( void )
  110. {
  111.   while ( First )
  112.     delete First;
  113. }
  114.  
  115.  
  116. int
  117. iter::traverse( trOp op )
  118. {
  119.   if ( mylist.firstNode() == 0 )
  120.     return TR_EMPTY;
  121.  
  122.   int rc = TR_OK;
  123.  
  124.   switch ( op )
  125.   {
  126.  
  127.   case NEXT:
  128.     if ( nptr )
  129.     {
  130.  
  131.       nptr = nptr->Next;
  132.       break;
  133.     }
  134.  
  135.   case FIRST:
  136.     nptr = mylist.firstNode();
  137.     break;
  138.  
  139.  
  140.   case PREV:
  141.     if ( nptr )
  142.     {
  143.       nptr = nptr->Prev;
  144.       break;
  145.     }
  146.  
  147.   case LAST:
  148.     nptr = mylist.lastNode();
  149.     break;
  150.  
  151.   case CURR:
  152.     break;
  153.  
  154.   }
  155.  
  156.   return ( nptr ) ? TR_OK : TR_NOMORE;
  157. }
  158.  
  159.  
  160.